package com.tecent.chat.actvity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;


import com.tecent.chat.R;
import com.tecent.chat.broadcast.LocalReceiver;


public class BroadcastActivity extends AppCompatActivity {

    private LocalReceiver localReceiver;
    private LocalBroadcastManager localbm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);

        localbm = LocalBroadcastManager.getInstance(this);
        IntentFilter intentFilter = new IntentFilter("com.tecent.chat.LOCAL_BROADCAST");
        localReceiver = new LocalReceiver();

        localbm.registerReceiver(localReceiver, intentFilter);

        // 点击按钮，发送本地广播
        findViewById(R.id.local1).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.tecent.chat.LOCAL_BROADCAST");
                localbm.sendBroadcast(intent);
            }
        });


        // 点击按钮，强制下线
        findViewById(R.id.local2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v("main", "click off");
                Intent intent = new Intent("com.tecent.chat.FORCE_OFFLINE");
                sendBroadcast(intent);
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        localbm.unregisterReceiver(localReceiver);
    }
}
